#ifndef WSIM_CLIENT_H
#define WSIM_CLIENT_H
#include <string>
#include <wsim/websocket_common.h>
#include <map>
#include <QEvent>
#include <QWidget>

#include <QObject>
#include <QThread>
#include "../service/wsim.h"
#include <QList>
#include <QThread>
#include <jsoncpp/json/json.h>
#include "./myconfig.h"

extern mycfg* pConfig;
class WSIM_Event : public QEvent
{
public:
    WSIM_Event(Type e);
        WSIM_Event(Type e,int f);
    WSIM_Event(Type e,int f,WebsocketData_Type t,char *p,unsigned int l);
    ~WSIM_Event();
public:
    static Type m_WS_event;
    static Type m_WS_close;
public:
    int fd;
    WebsocketData_Type datatype;
    char *buf;
    unsigned int bufLen;
};

enum{
    BINTYPE_NULL=0,//空
    BINTYPE_BYTE=1,//字节块
    BINTYPE_FILE=2,//文件
    BINTYPE_TEAMVIEW=3,//远程协助

}BINDATA_Type;
/**
 * @brief The link_item class
 * 沟通项
 */
class linkItem{
public:
    int tfd;
    int ffd;
    int magic;
    int binType;//1块 2文件
    std::string label;
    std::string filename;    
    int pageIndex=0;//包序号从1开始
    int maxPageNum=1;    
    int maxPageLen;//包大小 每包一样   总大小<=maxPageNum*maxPageLen
};

static QList<linkItem> m_linkList;


class WSIM_client{
private:
    static int wsfd;
    static int m_pageSize;

private:
    static int execJson(Json::Value sendRoot);
public:

    static void setfd(int fd);
    static void setPageSize(int size);
    static void closefd();
    static int getPageSize();    
    static void sendTEXT(std::string toname, std::string text);
    static void sendTEXT_uuid(std::string uuid, std::string text);
    static void sendTEXT_fd(int fd,std::string text);
    static void sendRequest(int tofd,int magic,int type,std::string text);
    static void sendResponse(int tofd,int magic,std::string response);
    static int sendBYTE(wsimBIN_head head, char *buf,unsigned int bufLen);
    static void delayms(int ms);

    static void sendTeamCommand(int tofd,int magic,std::string command);
    static void hi();
    static void join(std::string nickname,std::string password);
    static void reMood(std::string mood);
    static void regUser(std::string nickname,std::string pwd);
    static void rePassword(std::string nickname,std::string oldpwd,std::string newpwd);
    static void reOrgan(std::string nickname,std::string organ);
    static void reName(std::string nickname,std::string name);
    static void reMobie(std::string nickname,std::string val);
    static void readUserInfo(std::string nickname);
    static void readClients();
    static void readRegClients();
    static void readNoRegClients();
    static void readOrganUser(std::string nickname);
    static void readGroupUser(std::string nickname);

    static void addLinkUp(int tfd,int ffd,int magic,std::string filename);
    static void addTeamView(int tfd,int ffd,int magic,std::string filename);
    static void addLinkItem(linkItem item);
    static void delLinkUp(int tfd,int ffd,int magic);
    static int findLinkUp(int tfd,int ffd,int magic);
    static linkItem getLinkUp(int index);


};



int wsim_init(std::string ip,int prot);
void setObj(QObject *obj);
#endif // WSIM_CLIENT_H
